package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

import org.apache.commons.lang.StringUtils;


public class SqlBuilder {

	private static String inputFilePath = "E:/aca_orig.sql";
	private static String outputFilePath = "E:/aca.sql";
	public static void main(String[] args) {
		BufferedReader in = null;
		BufferedWriter out = null;
		try{
			in = new BufferedReader(new FileReader(inputFilePath));
			out = new BufferedWriter(new FileWriter(outputFilePath));
			String originalSql = "";
			while(in.ready()){
				originalSql += in.readLine() + " ";
			}
			
			originalSql = originalSql.replaceAll("`", "");
			originalSql = originalSql.replaceAll(" AS "," ");
			originalSql = originalSql.replaceAll(" as "," ");
			String sql = "";
			boolean isLastEmpty = false;
			for(int i = 0;i<originalSql.length();i++){
				String c = String.valueOf(originalSql.charAt(i));
				System.out.println(c);
				if(StringUtils.isBlank(c)){
					if(!isLastEmpty){
						isLastEmpty = true;
						sql += " ";
					}
				}else{
					sql += c;
					isLastEmpty = false;
				}
			}
			
			System.out.println(sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{if(in != null)in.close();}catch(Exception e){}
			try{if(out != null)out.close();}catch(Exception e){}
		}
	}

}
